package com._520it.crm.service.impl;

import com._520it.crm.domain.OrderBill;
import com._520it.crm.domain.PaymentConfirm;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.mapper.PaymentConfirmMapper;
import com._520it.crm.mapper.PaymentHandleMapper;
import com._520it.crm.service.IPaymentConfirmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class PaymentConfirmServiceImpl implements IPaymentConfirmService {
    @Autowired
    private PaymentConfirmMapper paymentConfirmMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;
    @Autowired
    private PaymentHandleMapper paymentHandleMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(PaymentConfirm pc) {
        int count = paymentConfirmMapper.insert(pc);
        return count;
    }

    @Override
    public PaymentConfirm selectByPrimaryKey(Long id) {
        return paymentConfirmMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<PaymentConfirm> selectAll() {
        return paymentConfirmMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(PaymentConfirm record) {
        int count = paymentConfirmMapper.updateByPrimaryKey(record);
        return count;
    }

    @Override
    public void confirm(Long orderBillId) {
        PaymentConfirm pc = new PaymentConfirm();
        pc.setOrderBillId(orderBillId);
        pc.setInsurancePolicyNumber(String.valueOf(System.currentTimeMillis()));
        pc.setPaymentDate(new Date());
        orderBillMapper.updateStateAndRemark(orderBillId, OrderBill.STATE_PAYYES,"缴费成功");
        paymentConfirmMapper.insert(pc);

        Long handleId =  paymentHandleMapper.selectByOrderBillId(orderBillId);
        paymentHandleMapper.updateserialNumber("LSH"+System.currentTimeMillis(),handleId);
        paymentConfirmMapper.insertRelation(pc.getId(),handleId);
    }

    @Override
    public void confirmFailed(Long orderBillId) {
        orderBillMapper.updateStateAndRemark(orderBillId, OrderBill.STATE_PAYNO,"缴费失败,原因:余额不足");
    }
}
